/**
 * @file ctrlZee_OSAL.c
 * @brief OSAL related implementation for the CtrlZee module.
 * @author Vamshi Ponnapalli
 */

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "ZComDef.h"
#include "hal_drivers.h"
#include "OSAL.h"
#include "OSAL_Tasks.h"

#if defined ( MT_TASK )
  #include "MT.h"
  #include "MT_TASK.h"
#endif

#include "nwk.h"
#include "APS.h"
#include "ZDApp.h"
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  #include "ZDNwkMgr.h"
#endif
#if defined ( ZIGBEE_FRAGMENTATION )
  #include "aps_frag.h"
#endif

#include "ctrlZee.h"

/*******************************************************************************
 * Variable declarations
 ******************************************************************************/
/*
 * The order in this table must be identical to the task initialization calls 
 * below in osalInitTask.
 */
const pTaskEventHandlerFn tasksArr[] = 
{
  macEventLoop
  ,nwk_event_loop
  ,Hal_ProcessEvent

#if defined( MT_TASK )
  ,MT_ProcessEvent
#endif

  ,APS_event_loop

#if defined ( ZIGBEE_FRAGMENTATION )
  ,APSF_ProcessEvent
#endif

  ,ZDApp_event_loop

#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
  ,ZDNwkMgr_event_loop
#endif

  ,ctrlZee_controlProcessEvent
};

const uint8 tasksCnt = sizeof(tasksArr) / sizeof(tasksArr[0]);
uint16 *tasksEvents;

/*******************************************************************************
 * Function implementations
 ******************************************************************************/
/*
 * This function invokes the initialization function for each task.
 */
void osalInitTasks(void)
{
  uint8 taskId = 0;

  tasksEvents = (uint16*)osal_mem_alloc(sizeof(uint16) * tasksCnt);
  osal_memset(tasksEvents, 0, (sizeof(uint16) * tasksCnt));

  macTaskInit(taskId++);
  nwk_init(taskId++);
  Hal_Init(taskId++);
  
#if defined(MT_TASK)
  MT_TaskInit(taskId++);
#endif
  
  APS_Init(taskId++);

#if defined (ZIGBEE_FRAGMENTATION)
  APSF_Init(taskId++);
#endif
  
  ZDApp_Init( taskId++ );

#if defined (ZIGBEE_FREQ_AGILITY) || defined (ZIGBEE_PANID_CONFLICT)
  ZDNwkMgr_Init(taskId++);
#endif
  
  ctrlZee_controlInit(taskId++);
}
